//
//  ZCSRecommendViewController.m
//  聆听FM
//
//  Created by dllo on 16/6/6.
//  Copyright © 2016年 Dark. All rights reserved.
//

#import "ZCSRecommendViewController.h"
#import "ZCSRecommendModel.h"
#import "ZCSRecommendTVC.h"
#import "ZCSHearTableViewCell.h"
#import "ZCSReHeaderTableViewCell.h"
#import "ZCSHotRecomListModel.h"
#import "DCPicScrollView.h"
#import "ZCSFocusImagesModel.h"
#import "ZCSReFindNewView.h"
#import "ZCSReDiscoveryListModel.h"
#import "ZCSTipsRecomdViewController.h"
#import "ZCSBoutiqueListenViewController.h"
#import "ListenNewsViewController.h"
#import "AlbumDetailViewController.h"
#import "ZCSRecommendEditTVC.h"
#import "ZCSListenListDetailViewController.h"
#import "ZCSListenListListModel.h"
#import "ZCSPlayMusicViewController.h"
#import "YWTheMostHotestViewController.h"
#import "YWSubscribeListPageViewController.h"
#import "YWTheMostSubscribeViewController.h"
#import "YWCirclePhotoViewController.h"
#import "CBStoreHouseRefreshControl.h"

@interface ZCSRecommendViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, retain) ZCSRecommendModel *recommendModel;
@property (nonatomic, retain) DCPicScrollView *picView;
//推荐的TableView
@property (nonatomic, retain) UITableView *recommendTabelView;
//轮播图数据源
@property (nonatomic, retain) NSMutableArray *scrollArray;
@property (nonatomic, retain) ZCSReFindNewView *findView;
//发现新奇数据源
@property (nonatomic, retain) NSMutableArray *findNewArray;
@property (nonatomic, strong) CBStoreHouseRefreshControl *storeHouseRefreshControl;
@end

@implementation ZCSRecommendViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor clearColor];
    self.navigationController.navigationBar.translucent = YES;
    self.navigationController.navigationBar.hidden = YES;
    [self getView];
    [self getData];
    
    
}
#pragma -- 获得数据
- (void)getData
{
    [self getRecomData];
}

#pragma mark -- 创建视图
- (void)getView
{
    [self initTableView];
}

- (void)creatTVHeaderViewWith:(NSArray *)images
{
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 300 * FITHEIGHT)];
    self.picView = [DCPicScrollView picScrollViewWithFrame:CGRectMake(0, 0, SCREENWIDTH, 200 * FITHEIGHT) WithImageUrls:images];
    self.picView.placeImage = [UIImage sd_animatedGIFNamed:@"占位图"];
    __weak ZCSRecommendViewController *rvc = self;
    [self.picView setImageViewDidTapAtIndex:^(NSInteger index) {
        ZCSFocusImagesModel *model = [rvc.recommendModel.focusImagesArray objectAtIndex:index];
        //专辑
        if (model.type == 2) {
            AlbumDetailViewController *advc = [[AlbumDetailViewController alloc] init];
            advc.albmId = model.albumId;
            advc.hidesBottomBarWhenPushed = YES;
            [rvc.navigationController pushViewController:advc animated:YES];
        }
        //听单
        if (model.type == 9) {
            ZCSListenListDetailViewController *list = [[ZCSListenListDetailViewController alloc] init];
            list.ID = model.specialId;
            list.type = [NSString stringWithFormat:@"%ld", model.subType];
            rvc.hidesBottomBarWhenPushed = YES;
            [rvc.navigationController pushViewController:list animated:YES];
        }
        //歌单
        if (model.type == 3) {
            NSMutableArray *tempArray = [NSMutableArray array];
            ZCSListenListListModel *playModel = [[ZCSListenListListModel alloc] init];
            playModel.id = model.trackId;
            playModel.title = model.longTitle;
            [tempArray addObject:playModel];
            ZCSPlayMusicViewController *pmvc = [ZCSPlayMusicViewController sharePlayVC];
            pmvc.dataArray = tempArray;
            pmvc.maxPage = 1;
            pmvc.musicIndex = 0;
            [rvc presentViewController:pmvc animated:YES completion:nil];
        }
    }];
    self.picView.AutoScrollDelay = 3.0;
    
    [header addSubview:_picView];
    
    self.findView = [[ZCSReFindNewView alloc] initWithFrame:CGRectMake(0, 200 * FITHEIGHT, SCREENWIDTH, 90 * FITHEIGHT)];
    
    self.findView.reFindBlock = ^(NSInteger index)
    {
        ZCSReDiscoveryListModel *model = [rvc.findNewArray objectAtIndex:index];
        switch (index) {
            case 0:
            {
                AlbumDetailViewController *advc = [[AlbumDetailViewController alloc] init];
                advc.albmId = model.propertiesModel.albumId;
                advc.hidesBottomBarWhenPushed = YES;
                [rvc.navigationController pushViewController:advc animated:YES];
            }
                break;
            case 1:
            {
                YWTheMostHotestViewController *theMost = [[YWTheMostHotestViewController alloc] init];
                theMost.titleString = @"今日最火";
                theMost.hidesBottomBarWhenPushed = YES;
                [rvc.navigationController pushViewController:theMost animated:YES];
            }
                break;
            case 2:
            {
                YWTheMostSubscribeViewController *subsribe = [[YWTheMostSubscribeViewController alloc] init];
                subsribe.titleString = @"经典必听";
                subsribe.hidesBottomBarWhenPushed = YES;
                [rvc.navigationController pushViewController:subsribe animated:YES];
                
            }
                
                break;
            case 3:
            {
                YWCirclePhotoViewController *cir = [[YWCirclePhotoViewController alloc] init];
                cir.key = model.propertiesModel.key;
                cir.titleString = @"热门分享";
                cir.hidesBottomBarWhenPushed = YES;
                [rvc.navigationController pushViewController:cir animated:YES];
            }
                break;
                
            default:
                break;
        }
        
    };
    [header addSubview:_findView];
    
    self.recommendTabelView.tableHeaderView = header;
    
    
}

#pragma mark -- 初始化主体tableView
- (void)initTableView
{
    self.recommendTabelView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT - 49 - 64) style:UITableViewStyleGrouped];
    self.recommendTabelView.delegate = self;
    self.recommendTabelView.dataSource = self;
    self.recommendTabelView.sectionFooterHeight = 10;
    self.recommendTabelView.backgroundColor = [UIColor clearColor];
    self.recommendTabelView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_recommendTabelView];
    
    //注册cell
    [self.recommendTabelView registerNib:[UINib nibWithNibName:@"ZCSRecommendTVC" bundle:nil] forCellReuseIdentifier:@"reTVC"];
    [self.recommendTabelView registerNib:[UINib nibWithNibName:@"ZCSRecommendEditTVC" bundle:nil] forCellReuseIdentifier:@"reTVC1"];
    
    [self.recommendTabelView registerNib:[UINib nibWithNibName:@"ZCSHearTableViewCell" bundle:nil] forCellReuseIdentifier:@"hearTVC"];
    [self.recommendTabelView registerNib:[UINib nibWithNibName:@"ZCSReHeaderTableViewCell" bundle:nil] forCellReuseIdentifier:@"reHeader"];
//    //下拉刷新:
//    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//        [self reFresh];
//    }];
//    [header setTitle:@"下拉可刷新..." forState:MJRefreshStateIdle];
//    [header setTitle:@"释放可刷新..." forState:MJRefreshStatePulling];
//    [header setTitle:@"正在加载中..." forState:MJRefreshStateRefreshing];
//    self.recommendTabelView.mj_header = header;
    
    
    self.storeHouseRefreshControl = [CBStoreHouseRefreshControl attachToScrollView:self.recommendTabelView target:self refreshAction:@selector(refreshTriggered:) plist:@"LTFM" color:[UIColor whiteColor] lineWidth:3 dropHeight:80 scale:1.2 horizontalRandomness:300 reverseLoadingAnimation:NO internalAnimationFactor:0.7];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [self.storeHouseRefreshControl scrollViewDidScrollWithType:0];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [self.storeHouseRefreshControl scrollViewDidEndDragging];
}

- (void)refreshTriggered:(id)sender
{
    
    [self performSelector:@selector(finishRefreshControl) withObject:nil afterDelay:2 inModes:@[NSRunLoopCommonModes]];
}
- (void)finishRefreshControl
{
    [self reFresh];
}



#pragma mark -- 刷新
- (void)reFresh
{
    self.recommendModel = [[ZCSRecommendModel alloc] init];
    [self.recommendTabelView reloadData];
    
    [self getRecomData];
}

#pragma mark -- tableView代理方法
#pragma mark -- 分区头视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    ZCSReHeaderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reHeader"];
    cell.moreLabel.userInteractionEnabled = YES;
    cell.moreLabel.tag = 10000 + section;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(moreLabelAction:)];
    [cell.moreLabel addGestureRecognizer:tap];
    switch (section) {
        case 0:
        {
            cell.headerLabel.text = self.recommendModel.editorModel.title;
            
        }
            break;
        case 1:
            cell.headerLabel.text = self.recommendModel.specialModel.title;
            break;
        default:
        {
            ZCSHotRecomListModel *model = [self.recommendModel.hotRecomModel.list objectAtIndex:section - 2];
            cell.headerLabel.text = model.title;
        }
            
            break;
    }
    
    return cell;
}
- (void)moreLabelAction:(UITapGestureRecognizer *)tap
{
    NSInteger num = tap.view.tag - 10000;
    switch (num) {
        case 0:
        {
            ZCSTipsRecomdViewController *trVc = [[ZCSTipsRecomdViewController alloc] init];
            
            trVc.titleString = @"小编推荐";
            trVc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:trVc animated:YES];
        }
            break;
        case 1:
        {
            ZCSBoutiqueListenViewController *blvc = [[ZCSBoutiqueListenViewController alloc] init];
            blvc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:blvc animated:YES];
            
        }
            break;
            
        default:
        {
            
            ZCSHotRecomListModel *model = [self.recommendModel.hotRecomModel.list objectAtIndex:num - 2];
            ListenNewsViewController *lnvc = [[ListenNewsViewController alloc] init];
            lnvc.categoryId = model.categoryId;
            lnvc.titleString = model.title;
            lnvc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:lnvc animated:YES];
            
        }
            break;
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40 * FITHEIGHT;
}

#pragma mark  -----分区 数量
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    //NSLog(@"%ld", self.recommendModel.hotRecomModel.list.count);
    return self.recommendModel.hotRecomModel.list.count + 2;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 1) {
        ZCSSpecialColumnList *model = [self.recommendModel.specialModel.list objectAtIndex:indexPath.row];
        ZCSListenListDetailViewController *lldvc = [[ZCSListenListDetailViewController alloc] init];
        lldvc.type = model.contentType;
        lldvc.ID = model.specialId;
        lldvc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:lldvc animated:YES];
    }
}


#pragma mark  ------cell 数量

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 1) {
        return 2;
    }
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    __weak ZCSRecommendViewController *rvc = self;
    if (indexPath.section == 0) {
        ZCSRecommendEditTVC *cell = [tableView dequeueReusableCellWithIdentifier:@"reTVC1"];
        cell.editorModel = self.recommendModel.editorModel;
        cell.rBlock = ^(LTAlbumModel *model)
        {
            AlbumDetailViewController *advc = [[AlbumDetailViewController alloc] init];
            advc.albmId = model.albumId;
            advc.albumModel = model;
            advc.hidesBottomBarWhenPushed = YES;
            [rvc.navigationController pushViewController:advc animated:YES];
        };
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    if (indexPath.section == 1) {
        ZCSHearTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"hearTVC"];
        cell.listModel = [self.recommendModel.specialModel.list objectAtIndex:indexPath.row];
        
        return cell;
    }
    
    ZCSRecommendTVC *cell = [tableView dequeueReusableCellWithIdentifier:@"reTVC"];
    cell.rBlock = ^(LTAlbumModel *model)
    {
        AlbumDetailViewController *advc = [[AlbumDetailViewController alloc] init];
        advc.albmId = model.albumId;
        advc.albumModel = model;
        advc.hidesBottomBarWhenPushed = YES;
        [rvc.navigationController pushViewController:advc animated:YES];
    };
    cell.hotRecomListModel = [self.recommendModel.hotRecomModel.list objectAtIndex:indexPath.section - 2];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}




- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1) {
        return 100 * FITHEIGHT;
    }
    else
    {
        return 190 * FITHEIGHT;
    }
}





#pragma mark -- 请求数据

- (void)getRecomData
{
    NSString *urlString = @"http://mobile.ximalaya.com/mobile/discovery/v2/recommends?channel=and-d10&device=android&includeActivity=true&includeSpecial=true&scale=2&statEvent=pageview%2Ftab%40%E5%8F%91%E7%8E%B0_%E6%8E%A8%E8%8D%90&version=4.3.92";
    __weak ZCSRecommendViewController *rvc = self;
    [LTNetTool GetNetWithURL:urlString Body:nil Header:nil Response:0 Success:^(id result) {
        rvc.recommendModel = [[ZCSRecommendModel alloc] initWithDic:result];
        rvc.scrollArray = [NSMutableArray array];
        rvc.findNewArray = [NSMutableArray array];
        for (ZCSFocusImagesModel *model in rvc.recommendModel.focusImagesArray) {
            [rvc.scrollArray addObject:model.pic];
        }
        for (ZCSReDiscoveryListModel *disModel in rvc.recommendModel.reDiscoveryModel.list) {
            [rvc.findNewArray addObject:disModel];
        }
        
        if (rvc.scrollArray.count) {
            static dispatch_once_t onceToken;
            dispatch_once(&onceToken, ^{
                [rvc creatTVHeaderViewWith:rvc.scrollArray];
                rvc.findView.dataArray = rvc.findNewArray;
            });
            
        }
        [rvc.recommendTabelView reloadData];
        [rvc.storeHouseRefreshControl finishingLoading];
    } Failure:^(NSError *error) {
        [rvc.storeHouseRefreshControl finishingLoading];
        NSLog(@"%@", error);
    }];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
